package tiandang.core.utils;

import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WX_TemplateMsgUtil {

  private static Logger log = LoggerFactory.getLogger(WX_TemplateMsgUtil.class);

  public static String sendWechatMsgToUser(String touser, String templatId, String page, String formId, JSONObject data, String emphasisKeyword, String token) {
    String tmpurl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" + token;

    JSONObject json = new JSONObject();
    json.put("touser", touser);
    json.put("form_id", formId);
    json.put("page", page);
    json.put("template_id", templatId);
    json.put("data", data);
    json.put("emphasis_keyword", emphasisKeyword);

    try {
      JSONObject resultJson = WX_HttpsUtil.httpsRequest(tmpurl, "POST", json.toString());
      // JSONObject resultJson = new JSONObject(result);
      log.info("发送微信消息返回信息：" + resultJson.get("errcode"));
      String errmsg = (String) resultJson.get("errmsg");
      if (!"ok".equals(errmsg)) {  //如果为errmsg为ok，则代表发送成功，公众号推送信息给用户了。
        return "error";
      }
    } catch (Exception e) {
      e.printStackTrace();
      return "error";
    }
    return "success";
  }

}
